package org.jfk.core.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.jfk.core.utils.I18nUtil;
import org.jfk.sys.menu.Menu;
import org.jfk.sys.user.User;
import org.jfk.tbox.model.TboxMsgLog;

import com.jfinal.kit.StrKit;

public class IndexController extends BaseController {
	
	public void index(){
		redirect("/login");
	}
	
	public void main(){
		Subject subject = SecurityUtils.getSubject();
		boolean rememberMe = subject.isRemembered();
		String loginName = (String)subject.getPrincipal();
		
		User user = User.dao.findByLoginName(loginName);
		Menu root = Menu.dao.loadByRoot(user.getInt("id"));

		//屏蔽无权限用户
		List children = root.get("children");
		if (children == null || children.size() == 0){
        	super.setAttr(ERR_MSG, I18nUtil.getValueByKey("admin.login.error.locked_user"));
        	super.setSessionAttr(ERR_MSG, I18nUtil.getValueByKey("admin.login.error.locked_user"));
        	super.renderJsp("/login.jsp");
            return;
		}		
		
		int undisposeMsgCount = TboxMsgLog.dao.getUndisposeMsgCount();
		
		super.setAttr("root", root);
		super.setAttr("undisposeMsgCount", undisposeMsgCount);
		
		super.getSession().setAttribute("currUser", user);
		super.setAttr("currUser", user);
		super.renderJsp("/index.jsp");
	}
	
	public void login(){
		super.setAttr("user",this.getCookie("user"));
		super.setAttr("password",this.getCookie("password"));
		String errMsg = super.getSessionAttr(ERR_MSG);
		if (StrKit.notBlank(errMsg)){
			super.setAttr(ERR_MSG,errMsg);
		}		
	}
	
	public void logout(){
		Subject user = SecurityUtils.getSubject();
		if (user != null){
			user.logout();
		}
		super.redirect("/login");
	}
	
	public void nologin(){
		super.setAttr(ERR_MSG, I18nUtil.getValueByKey("admin.login.error.nologin"));
	}
	
	public void doLogin(){
        String username = getPara("loginName");
        String password = getPara("password");
        String remember_me = super.getPara("remember_me");
        String needRandom = super.getPara("needRandom");

        boolean result = validateCaptcha("inputRandomCode"); // inputTandomCode为input中的name
        if ("1".equals(needRandom) && !result){
        	super.setAttr(ERR_MSG, I18nUtil.getValueByKey("admin.login.error.randomCode"));
        	super.render("/login.jsp");
            return;
        }
        
        boolean rememberMe = remember_me==null?false:true;         
        try{
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);

            token.setRememberMe(rememberMe);

            Subject user = SecurityUtils.getSubject();
            user.login(token);
            
        }catch (UnknownAccountException uae) {
        	uae.printStackTrace();
        	super.setAttr(ERR_MSG, uae.getMessage());
        	super.render("/login.jsp");
            return;
        }catch (IncorrectCredentialsException   ice) {
        	ice.printStackTrace();
        	super.setAttr(ERR_MSG, I18nUtil.getValueByKey("admin.login.error.password_err"));
        	super.render("/login.jsp");
            return;
        }catch (LockedAccountException  lae) {
        	lae.printStackTrace();
        	super.setAttr(ERR_MSG, I18nUtil.getValueByKey("admin.login.error.locked_user"));
        	super.render("/login.jsp");
            return;
        }catch (ExcessiveAttemptsException  eae) {
        	eae.printStackTrace();
        	super.setAttr(ERR_MSG, I18nUtil.getValueByKey("admin.login.error.too_more_try"));
        	super.render("/login.jsp");
            return;
        }catch (AuthenticationException e) {
        	e.printStackTrace();
        	super.setAttr(ERR_MSG, e.getMessage());
        	super.render("/login.jsp");
            return;
        }
        //登录成功
        if (rememberMe){
        	this.setCookie("user", username, 24*60*60*7);
        	this.setCookie("password", password, 24*60*60*7);
        }
        redirect("/main");			
	}
	
	public void noperms(){
		
	}
	
	public void randomCode(){
		this.renderCaptcha();
	}	
}
